package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.MessageConstant;
import com.sky.dto.OrdersCancelDTO;
import com.sky.dto.OrdersConfirmDTO;
import com.sky.dto.OrdersPageQueryDTO;
import com.sky.dto.OrdersRejectionDTO;
import com.sky.entity.OrderDetail;
import com.sky.entity.Orders;
import com.sky.exception.OrderBusinessException;
import com.sky.mapper.OrdermmMapper;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.OrdermmService;
import com.sky.vo.OrderStatisticsVO;
import com.sky.vo.OrderVO;
import org.apache.commons.collections4.BagUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.PutMapping;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class OrdermmServiceImpl implements OrdermmService {
    /*
     * 查询订单详情
     * */
    @Autowired
    private OrdermmMapper ordermmMapper;

    @Override
    public PageResult apihistorylist(OrdersPageQueryDTO ordersPageQueryDTO) {
        PageHelper.startPage(ordersPageQueryDTO.getPage(), ordersPageQueryDTO.getPageSize());
        Page<Orders> page = ordermmMapper.listhistorder(ordersPageQueryDTO);
        long total = page.getTotal();
        List<Orders> result = page.getResult();
        PageResult pageResult = new PageResult();
        pageResult.setTotal(total);
        pageResult.setRecords(result);
        return pageResult;
    }

    /*
     * 查询状态数量
     * */
    @Override
    public OrderStatisticsVO listcount() {
        OrderStatisticsVO orderStatisticsVO = ordermmMapper.listcount();
        return orderStatisticsVO;
    }
/*
* 接单
* */
    @Override
    public void updateBid(OrdersConfirmDTO confirmDTO) {
        Orders orders = new Orders();
        BeanUtils.copyProperties(confirmDTO,orders);
        orders.setStatus(Orders.CONFIRMED);
        ordermmMapper.updateByid(orders);
    }
/*
* 拒单
* */
    @Override
    public void jection(OrdersRejectionDTO ordersRejectionDTO) {
        Long id = ordersRejectionDTO.getId();
       Orders orderDB = ordermmMapper.selecttBid(id);
       if (orderDB==null || !orderDB.getStatus().equals(Orders.TO_BE_CONFIRMED)){
           throw  new OrderBusinessException(MessageConstant.ORDER_STATUS_ERROR);
       }
       //查询支付状态
        Integer payStatus = orderDB.getPayStatus();
       if (payStatus == Orders.PAID){
           //TODO 为用户退钱
       }
        //修改拒单原因和状态还有拒单时间
        Orders orders = new Orders();
        BeanUtils.copyProperties(ordersRejectionDTO,orders);
        orders.setStatus(Orders.CONFIRMED);
        orders.setCancelTime(LocalDateTime.now());

        ordermmMapper.jiedanupdate(orders);

    }
/*
* 取消订单
* */
    @Override
    public void cancel(OrdersCancelDTO ordersCancelDTO) {
        Long id = ordersCancelDTO.getId();
       Orders orderDB = ordermmMapper.selecttBid(id);
       if (orderDB.getPayStatus()==Orders.PAID){
           //TODO 用户已经支付需要退钱
       }
        // 管理端取消订单需要退款，根据订单id更新订单状态、取消原因、取消时间
        Orders orders = new Orders();
        BeanUtils.copyProperties(ordersCancelDTO,orders);
        orders.setStatus(Orders.CANCELLED);
        orders.setCancelTime(LocalDateTime.now());
        ordermmMapper.cancel(orders);
    }
/*
* 派送订单
* */
    @Override
    public void delivery(long id) {
       Orders ordersDB = ordermmMapper.selecttBid(id);
       //判断订单是否为空和状态
        if (ordersDB == null || !ordersDB.getStatus().equals(Orders.CONFIRMED)){
            throw new OrderBusinessException(MessageConstant.ORDER_STATUS_ERROR);
        }
        //修改订单状态为派送中
        Orders orders = new Orders();
        orders.setId(id);
        orders.setStatus(Orders.DELIVERY_IN_PROGRESS);
        ordermmMapper.updateByid(orders);
    }
/*
* 完成订单
* */
    @Override
    public void finish(long id) {
       Orders orderDb = ordermmMapper.selecttBid(id);
       if (orderDb==null || !orderDb.getStatus().equals(Orders.DELIVERY_IN_PROGRESS)){
           throw new OrderBusinessException(MessageConstant.ORDER_STATUS_ERROR);
       }
        Orders orders = new Orders();
        orders.setId(id);
        orders.setStatus(Orders.COMPLETED);
        orders.setDeliveryTime(LocalDateTime.now());
        ordermmMapper.finish(orders);
    }



}

